package JavaSE.ThreadLearning.SynchronizedLearning.ProducerAndConsumer;

import java.util.Random;

/**
 *  生产者类
 *  如果笼子里有包子，则自我阻止
 *  如果笼子里没有包子，则生产包子，然后唤醒消费者
 */
public class Producer implements Runnable {
    // 蒸笼
    Box box;
    Food[] foods = {new Food("小笼包", 8), new Food("豆沙包", 2),
            new Food("鲜肉包", 3), new Food("牛肉包", 5)};
    Random random;
    // 构造函数
    public Producer(Box box) {
        this.box = box;
        random = new Random();
    }

    @Override
    public void run() {
        while (true) {
            // 生产者只做一件事情 生产包子
            int index = random.nextInt(foods.length);
            box.setFood(foods[index]);
        }
    }
}
